{
 "metadata": {
  "name": "identitysearch_example"
 },
 "nbformat": 2,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "\"\"\"Demonstration of quantum gate identity search.\"\"\"",
      "",
      "from sympy.physics.quantum.gate import (X, Y, Z, H, S, T, CNOT,",
      "        IdentityGate, CGate, gate_simp)",
      "from sympy.physics.quantum.identitysearch import *",
      "from sympy.physics.quantum.dagger import Dagger"
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "# Declare a few quantum gates",
      "x = X(0)",
      "y = Y(0)",
      "z = Z(0)",
      "h = H(0)",
      "cnot = CNOT(1,0)",
      "cgate_z = CGate((0,), Z(1))"
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Start with the trivial cases",
      "gate_list = [x]",
      "",
      "bfs_identity_search(gate_list, 1, max_depth=2)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "set([GateIdentity(X(0), X(0))])"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "gate_list = [y]",
      "",
      "bfs_identity_search(gate_list, 1, max_depth=2)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "set([GateIdentity(Y(0), Y(0))])"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# bfs_identity_search looks for circuits that reduce to a",
      "# scalar value unless told otherwise.",
      "# The following list should produce 4 identities as a result.",
      "gate_list = [x, y, z]",
      "",
      "bfs_identity_search(gate_list, 2)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "set([GateIdentity(X(0), X(0)),",
        "     GateIdentity(Z(0), Z(0)),",
        "     GateIdentity(X(0), Y(0), Z(0)),",
        "     GateIdentity(Y(0), Y(0))])"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "gate_list = [x, y, z, h]",
      "",
      "bfs_identity_search(gate_list, 2)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "set([GateIdentity(Y(0), H(0), Y(0), H(0)),",
        "     GateIdentity(X(0), Y(0), X(0), Y(0)),",
        "     GateIdentity(X(0), Y(0), Z(0)),",
        "     GateIdentity(X(0), H(0), Z(0), H(0)),",
        "     GateIdentity(Z(0), Z(0)),",
        "     GateIdentity(X(0), X(0)),",
        "     GateIdentity(Y(0), Y(0)),",
        "     GateIdentity(X(0), Z(0), X(0), Z(0)),",
        "     GateIdentity(Y(0), Z(0), Y(0), Z(0)),",
        "     GateIdentity(H(0), H(0))])"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# One has the option to limit the max size of the circuit.",
      "# The default size is the size of the gate list.",
      "bfs_identity_search(gate_list, 2, max_depth=3)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "set([GateIdentity(X(0), X(0)),",
        "     GateIdentity(X(0), Y(0), Z(0)),",
        "     GateIdentity(Z(0), Z(0)),",
        "     GateIdentity(H(0), H(0)),",
        "     GateIdentity(Y(0), Y(0))])"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# One also has the option to find circuits that only reduce",
      "# to the Identity matrix rather than only scalar matrices.",
      "bfs_identity_search(gate_list, 2, identity_only=True)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "set([GateIdentity(X(0), X(0)),",
        "     GateIdentity(Z(0), Z(0)),",
        "     GateIdentity(H(0), H(0)),",
        "     GateIdentity(Y(0), Y(0))])"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "gate_list = [cnot, cgate_z, h]",
      "",
      "bfs_identity_search(gate_list, 2, max_depth=4)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "set([GateIdentity(CNOT(1,0), H(0), C((0),Z(1)), H(0)),",
        "     GateIdentity(H(0), H(0)),",
        "     GateIdentity(C((0),Z(1)), C((0),Z(1))),",
        "     GateIdentity(CNOT(1,0), CNOT(1,0))])"
       ]
      }
     ],
     "prompt_number": 14
    }
   ]
  }
 ]
}